//@ts-ignore
import * as math from 'mathjs'

interface Point {
    x: number;
    y: number;
}

interface Style {
    left: number;
    top: number;
    width: number;
    height: number;
    rotate: number;
}
type PointKey = 't' | 'b' | 'l' | 'r' | 'lt' | 'rt' | 'lb' | 'rb';
class EditorUtils {
    // 角度转弧度
    static angleToRadian(angle: number): number {
        return (angle * Math.PI) / 180;
    }

    // 计算根据圆心旋转后的点的坐标
    static calculateRotatedPointCoordinate(point: Point, center: Point, rotate: number): Point {
        const radian = this.angleToRadian(rotate);
        return {
            x: (point.x - center.x) * Math.cos(radian) - (point.y - center.y) * Math.sin(radian) + center.x,
            y: (point.x - center.x) * Math.sin(radian) + (point.y - center.y) * Math.cos(radian) + center.y,
        };
    }

    // 获取旋转后的点坐标
    static getRotatedPointCoordinate(style: Style, center: Point, name: PointKey): Point {
        const points = {
            t: { x: style.left + style.width / 2, y: style.top },
            b: { x: style.left + style.width / 2, y: style.top + style.height },
            l: { x: style.left, y: style.top + style.height / 2 },
            r: { x: style.left + style.width, y: style.top + style.height / 2 },
            lt: { x: style.left, y: style.top },
            rt: { x: style.left + style.width, y: style.top },
            lb: { x: style.left, y: style.top + style.height },
            rb: { x: style.left + style.width, y: style.top + style.height }
        };
        return this.calculateRotatedPointCoordinate(points[name], center, style.rotate);
    }

    // 求两点之间的中点坐标
    static getCenterPoint(p1: Point, p2: Point): Point {
        return {
            x: p1.x + (p2.x - p1.x) / 2,
            y: p1.y + (p2.y - p1.y) / 2
        };
    }

    static sin(rotate: number): number {
        return Math.abs(Math.sin(this.angleToRadian(rotate)));
    }

    static cos(rotate: number): number {
        return Math.abs(Math.cos(this.angleToRadian(rotate)));
    }

    static mod360(deg: number): number {
        return (deg + 360) % 360;
    }

    static changeStyleWithScale(value: number): number {return 0
        //store.state.canvasStyleData.scale
        const scale=1
        return math.multiply(value, math.divide(parseInt(String(scale), 10), 100));
    }

    static toPercent(val: number): string {
        return `${val * 100}%`;
    }
}

export default EditorUtils;
